<template>
  <h2>求和：{{ sum }}</h2>
  <button @click="sum++">sum++</button>
  <hr />
  <h2>姓名：{{ name }}</h2>
  <h2>年龄：{{ age }}</h2>
  <h2>职业：{{ job.j1.type }}</h2>
  <h2>薪资：{{ job.j1.salary }}K</h2>
  <h2 v-if="person.car">
    开的是：{{ person.car.name }}，价值：{{ person.car.price.toFixed(2) }}W
  </h2>
  <button @click="name += '~'">修改姓名</button>
  <br />
  <button @click="age++">修改年龄</button>
  <br />
  <button @click="job.j1.salary++">加薪</button>
  <br />
  <button @click="addCar">给人添加一台车</button>
  <br />
  <button @click="person.car.name += '!'">改车名</button>
  <br />
  <button @click="person.car.price += 0.1">改车价格</button>
  <br />
  <button @click="showRawPerson">输出原始的person</button>
</template>
<script>
import { ref, reactive, toRefs, toRaw ,markRaw} from "vue";
export default {
  name: "Demo",
  setup(props, context) {
    // 数据
    let sum = ref(0);
    let person = reactive({
      name: "张三",
      age: 18,
      job: {
        j1: {
          type: "前端",
          salary: 10,
        },
      },
    });

    //方法
    /* 
    toRaw()：返回 reactive 或 readonly 代理的原始对象。
    */
    function showRawPerson() {
      person = toRaw(person);
      console.log("原始的person", person);
    }
    
    /* 
    markRaw()：标记一个对象，使其永远不会转换为 proxy。返回对象本身。
    */
    function addCar() {
      const car = {
        name: "奔驰",
        price: 40,
      };
      person.car = markRaw(car);
      console.log(person);
    }
    
    // 返回一个对象供模版使用
    return {
      sum,
      person,
      ...toRefs(person),
      showRawPerson,
      addCar,
    };
  },
};
</script>

